<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/29
 * Time: 14:15
 */
namespace App\Service\Live;

use App\Constant\RedisKeyConstant;
use App\Service\Live\AlterService;
use App\Service\Live\OperateService;
use App\User;
use Illuminate\Support\Facades\Redis;
use SwooleTW\Http\Websocket\Facades\Room;
use App\Http\Model\Mysql\UserModel;

class ClientService extends BaseService
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * check user if block
     * @param string $room_name
     * @param string $user_id
     * @return mixed
     */
    public function inBlock($room_name = '', $user_id = '')
    {
        return Redis::SISMEMBER(RedisKeyConstant::ROOM_BLOCK_USER_PREFIX . $room_name, $user_id);
    }

    /**
     * put user into redis
     * @param string $user_id
     * @return array|mixed
     */
    public function setUser($user_id = '')
    {
        //check user info
        $user_info  =   Redis::HGET(RedisKeyConstant::USER_DETAIL_INFO, $user_id);
        if (empty($user_info)) {
            $user_find    =   UserModel::where(['uid' => $user_id])->select(['uid', 'nickname', 'headimg', 'phone'])->first();
            if (!empty($user_find)) {
                $user_detail['user_id']     =   $user_find->uid;
                $user_detail['nick_name']   =   $user_find->nickname;
                $user_detail['phone']       =   $user_find->phone ?? '';
                $user_detail['head_img']    =   $user_find->headimg;
                Redis::HSET(RedisKeyConstant::USER_DETAIL_INFO, $user_id, json_encode($user_detail, JSON_UNESCAPED_UNICODE));
                return $user_detail;
            } else {
                return [];
            }
        }
        return json_decode($user_info, true);
    }

    /**
     * client user visit
     * @param string $room_name
     * @param string $user_id
     * @return mixed
     */
    public function userVisit($room_name = '', $user_id = '')
    {
        //观看次数
        Redis::INCR(RedisKeyConstant::ROOM_WATCH_NUMBER_PREFIX . $room_name);

        //每个用户观看时长--开始统计
        Redis::HSET(RedisKeyConstant::USER_WATCH_TIME_PREFIX . $room_name . '_' . $user_id, 'start_time', time());

        //set去重后人数
        return Redis::SADD(RedisKeyConstant::ROOM_WATCH_PEOPLE_PREFIX . $room_name, $user_id);
    }

    /**
     * client user leave
     * @param string $room_name
     * @param string $user_id
     * @return mixed
     */
    public function userLeave($room_name = '', $user_id = '')
    {
        //每个用户观看时长--结束统计
        $start_time =   Redis::HGET(RedisKeyConstant::USER_WATCH_TIME_PREFIX . $room_name . '_' . $user_id, 'start_time');
        $end_time   =   time();

        Redis::HSET(RedisKeyConstant::USER_WATCH_TIME_PREFIX . $room_name . '_' . $user_id, 'end_time', $end_time);

        //set去重后在线总人数
        Redis::SREM(RedisKeyConstant::ROOM_VISIT_ONLINE_PREFIX . $room_name, $user_id);

        //观看时长存入链表
        if (!empty($start_time) && $end_time - $start_time > 5) {
            return Redis::LPUSH(RedisKeyConstant::USER_WATCH_LINE_PREFIX . $room_name . '_' . $user_id, $start_time . '-' . $end_time);
        }
        return true;
    }

    /**
     * client user watch
     * @param string $room_name
     * @param string $user_id
     * @return mixed
     */
    public function userWatch($room_name = '', $user_id = '')
    {
        return Redis::SADD(RedisKeyConstant::ROOM_WATCH_PREFIX . $room_name, $user_id);
    }

    /**
     * client room watch
     * @param string $room_name
     * @return mixed
     */
    public function getUserWatch($room_name = '')
    {
        return Redis::GET(RedisKeyConstant::ROOM_WATCH_NUMBER_PREFIX . $room_name);
    }

    /**
     * room get watch users
     * @param string $room_name
     * @return mixed
     */
    public function getVisitNum($room_name = '')
    {
        return Redis::SCARD(RedisKeyConstant::ROOM_WATCH_PEOPLE_PREFIX . $room_name);
    }

    /**
     * room get watch times
     * @param string $room_name
     * @return mixed
     */
    public function getWatchNumber($room_name = '')
    {
        return Redis::GET(RedisKeyConstant::ROOM_WATCH_NUMBER_PREFIX . $room_name);
    }

}
